package com.oba.king.businessprocess.sendordermanage;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.oba.base.dao.DBDataModel;
import com.oba.base.interfaces.IBaseBo;
import com.oba.base.service.BaseBean;
import com.oba.king.controlmanage.proxyproduct.ProxyProductCondition;
import com.oba.king.controlmanage.proxyproduct.ProxyProductDetails;
import com.oba.king.datalib.Customer;
import com.oba.king.datalib.RecieverInfo;
import com.oba.king.datalib.deliverytype.DeliveryType;
import com.oba.king.datalib.paymenttype.PaymentType;
import com.oba.king.datalib.productinfo.ProductInfo;
import com.oba.king.sysmanager.area.Area;

public class OrderApplyBean extends BaseBean<OrderApply, OrderApplyService>{

	private DBDataModel orderApplyData = DBDataModel.create();
	private OrderApply orderApply;
	
	//显示明细
	private boolean showdetails;
	
	
	//是否已经保存
	
	private boolean saved;
	
	//发货单产品
	private List<OrderProductDetails> orderProductList; 
	private OrderProductDetails orderProduct; 
	private DBDataModel selectProducts = DBDataModel.create();
	private List<SelectItem> productsList;
	private boolean showSelectProduct;
	private ProxyProductCondition orderProductCondition;
	private Integer amount;
	
	//显示已选的产品
	private DBDataModel showProductsData = DBDataModel.create();
	
	
	// 查询 条件
	private OrderApplyCondition orderApplyCondition;
	

	//已经选择的产品
	private List<ProductInfo> selectedProducts;
	
	//是否在录入页面 显示返回按钮
	private boolean showBack;
	// 客户信息选择列表
	private DBDataModel customerData = DBDataModel.create();
	private List<SelectItem> customerList;
	private List<SelectItem> queryCustomerList;
	private List<SelectItem> areaList;
	private List<SelectItem> queryAreaList;
	private List<SelectItem> recieverInfoList;
	private List<SelectItem> queryRecieverInfoList;
	private List<SelectItem> deliverTypeList;
	private List<SelectItem> queryDeliverTypeList;
	private List<SelectItem> paymentTypeList;
	private List<SelectItem> queryPaymentTypeList;
	
	private boolean showSelectCustomerPopup;
	private boolean showSelectDelieverTypePopup;
	private boolean showSelectPaymentTypePopup;
	
	
	private SelectCustomerCondition customerCondition;
    
	
	/**
	 * Tabbed placement, possible values are "top" and "bottom", the default is
	 * "bottom".
	 */
	private String tabPlacement = "top";
//	private boolean contractVisible =true;
//	private boolean productVisible;
	
	
	public void showDetails(){
		if (orderApplyData.isRowAvailable()) {
			orderApply = (OrderApply) orderApplyData.getRowData();
			showdetails=true;
			goTarget("/ordermanage/addOrderApply.jspx");
		}
		
	}
	
	public void backFromDetails(){
		showdetails=false;
		goTarget("/ordermanage/orderApply.jspx");
		
	}
	public void clearCustomerCondition(){
		customerCondition=new SelectCustomerCondition();
	}
	
	public void findByCondition(){
		customerData.find(getService().getDao().findCustomerCondition(
				customerCondition));
	}
	
	public void findByProductCondition(){
		selectProducts.find(getService().getDao().findProductCondition(selectedProducts,orderProductCondition,this.orderApply.getCustomer()),5);
	}
	public void clearOrderProductCondition(){
		orderProductCondition=new ProxyProductCondition();
	}
	
	public void closeShowSelectProduct(){
		this.showSelectProduct=false;
	}
	public void openShowSelectProduct(){
		if(this.orderApply.getCustomer()!=null){
			selectProducts.find(this.getService().getDao().findAllProxyProduct(selectedProducts,this.orderApply.getCustomer()),5);
		this.showSelectProduct=true;
		}
	}
	
	public void selectOneProduct(){
		if(selectProducts.isRowAvailable()){
			orderProduct=new OrderProductDetails();
			orderProduct.setNewBo(true);
			ProxyProductDetails tempDo=(ProxyProductDetails)selectProducts.getRowData();
			selectedProducts.add(tempDo.getProduct());
			orderProduct.setProductinfo((tempDo).getProduct());
			orderProduct.setProxyProduct(tempDo);
			orderProduct.setProductinfo(tempDo.getProduct());
			orderProduct.setAmount(tempDo.getAmount());
			orderProduct.setTotalSum(orderProduct.getAmount()*tempDo.getProxyUnitPrice());
			orderProductList.add(orderProduct);
			showProductsData.setWrappedData(orderProductList);
			
			double sum=0;
			for(int i=0; i<orderProductList.size();i++){
				sum+=orderProductList.get(i).getTotalSum();
			}
			orderApply.setTotalSum(sum);
		}
		
		this.showSelectProduct=false;
		
	}
	
	
	public void modifyProduct(){
		if(showProductsData.isRowAvailable()){
			orderProduct=(OrderProductDetails)showProductsData.getRowData();
			orderProduct.setChecked(true);
			}
	}
	
	public void deleteProduct(){
		
		if(showProductsData.isRowAvailable()){
			orderProduct=(OrderProductDetails)showProductsData.getRowData();
					orderProductList.remove(orderProduct);
					selectedProducts.remove(orderProduct.getProductinfo());
					showProductsData.setWrappedData(orderProductList);
					double sum=0;
					for(int i=0; i<orderProductList.size();i++){
						sum+=orderProductList.get(i).getTotalSum();
					}
					orderApply.setTotalSum(sum);
					if(saved ||!orderProduct.isNewBo()){
						this.getService().del(orderProduct);
					}
			}
	}
    
	public void confirmProduct(){
		if(showProductsData.isRowAvailable()){
			orderProduct=(OrderProductDetails)showProductsData.getRowData();
			orderProduct.setTotalSum(orderProduct.getAmount()*orderProduct.getProxyProduct().getProxyUnitPrice());
			double sum=0;
			for(int i=0; i<orderProductList.size();i++){
				sum+=orderProductList.get(i).getTotalSum();
			}
			orderApply.setTotalSum(sum);
			orderProduct.setChecked(false);
		}
	}
	
	
	public void closeShowSelectCustomer(){
		showSelectCustomerPopup=false;
	}
	

	public void doAddRow() {
//		saved=false;
//		orderProductCondition=new ProxyProductCondition();
//		showSelectProduct=false;
//		orderProductList =new ArrayList<OrderProductDetails>();
//		showProductsData.setWrappedData(orderProductList);
//		orderProduct=new OrderProductDetails();
//		orderProduct.setNewBo(true);
//		customerCondition=new SelectCustomerCondition();
//		orderApply = new OrderApply();
//		orderApply.setOrderStatus(1);
//		orderApply.setOrderStatus2(1);
//		orderApply.setNewBo(true);
		initAdd();
		goTarget("/ordermanage/addOrderApply.jspx");
	}

	public void doEditRow() {
		if (orderApplyData.isRowAvailable()) {
			orderApply = (OrderApply) orderApplyData.getRowData();
			saved=false;
			goTarget("/ordermanage/addOrderApply.jspx");
		}
	}

	public void doDeleteRow() {
		if (orderApplyData.isRowAvailable()) {
			orderApply = (OrderApply) orderApplyData.getRowData();
			getService().getDao().deleteOrderApply(orderApply);
			doFind();
		}
	}
	
	/**
	 * 返回代理协议页面
	 */
	public void returnback(){
		goTarget("/proxycontract/proxycontract.jspx");
	}

	public void auditRow(){
		if (orderApplyData.isRowAvailable()) {
			orderApply = (OrderApply) orderApplyData.getRowData();
			goTarget("/proxycontract/auditProxyContract.jspx");
		}
	}
	
//	public void auditReject(){
//		orderApply.setAuditingStatus(3);
//		getService().save(orderApply);
//		doFindAudit();
//		goTarget("/proxycontract/proxycontract.jspx");
//	}
	
//	public void auditPass(){
//		proxyContract.setAuditingStatus(2);
//		getService().save(proxyContract);
//		doFindAudit();
//		goTarget("/proxycontract/proxycontract.jspx");
//	}
	
	

	public void doFindAudit(){
		orderApplyData.find(getService().getDao().findNeedAudit());
	}

	public void doSaveRow() {
		orderApply.setUserInfo(this.getSysUser());
		getService().save(orderApply);
		for(int i=0;i<orderProductList.size();i++){
			orderProductList.get(i).setOrderApply(orderApply);
		}
		
		getService().getDao().saveList(orderProductList);
		saved=true;
	}

	@Override
	public String doFind() {
		orderApplyData.find(getService().getDao().findAll());
		return null;
	}

	public void clearOrderApplyCondition() {
		orderApplyCondition = new OrderApplyCondition();
	}

	public void findByOrder() {
		//if(isAudit){
			orderApplyData.find(getService().getDao().findByOrder(
					orderApplyCondition));
//		}else{
//			proxyContractData.find(getService().getDao().findByProxyContract(
//					proxyContractCondition));
//		}
	}

	@Override
	public void init() {
		orderProductCondition=new ProxyProductCondition();
		showdetails=false;
		showSelectCustomerPopup= false;
		showSelectDelieverTypePopup= false;
		showSelectPaymentTypePopup= false;
		
		showBack=true;
		//isAudit=false;
		orderApplyData.find(getService().getDao().findAll());
		orderApply = new OrderApply();
		orderApplyCondition = new OrderApplyCondition();
		initAddLists();
		initQueryLists();
		//isAudit=false;
	}
	
	public void initAudit(){
		showSelectCustomerPopup= false;
		showSelectDelieverTypePopup= false;
		showSelectPaymentTypePopup= false;
		
		showBack=true;
		//isAudit=true;
		orderApplyData.find(getService().getDao().findNeedAudit());
		orderApply = new OrderApply();
		orderApplyCondition = new OrderApplyCondition();
		initAddLists();
		initQueryLists();
	}
	
	public void initAdd(){
		showdetails=false;
		saved =false;
		orderProductCondition =new ProxyProductCondition();
		orderProductList =new ArrayList<OrderProductDetails>();
		showProductsData.setWrappedData(orderProductList);
		orderProduct=new OrderProductDetails();
		orderProduct.setNewBo(true);
		showSelectProduct=false;
		customerCondition=new SelectCustomerCondition();
		showSelectCustomerPopup= false;
		showSelectDelieverTypePopup= false;
		showSelectPaymentTypePopup= false;
		
		showBack=false;
		//isAudit=false;
		orderApply = new OrderApply();
		orderApply.setOrderStatus(1);
		orderApply.setOrderStatus2(1);
		orderApply.setUserInfo(this.getSysUser());
		orderApply.setNewBo(true);
		initAddLists();
		initQueryLists();
	}
	
	public void initAddLists(){
		selectedProducts=new ArrayList<ProductInfo>();
		customerList = new ArrayList<SelectItem>();
		recieverInfoList =new ArrayList<SelectItem>();
		customerData.find(getService().getDao().findAllCustomer());
        for(IBaseBo customer:customerData.getAllData()){
        	customerList.add(new SelectItem(((Customer)customer),((Customer)customer).getCustomerName()));
        }
		areaList = new ArrayList<SelectItem>(); 
		for (Area a : getService().getDao().findAllArea()) {
			areaList.add(new SelectItem(a, a.getAreaName()));
		}
		
		deliverTypeList = new ArrayList<SelectItem>(); 
		for (DeliveryType a : getService().getDao().findAllDeliveryType()) {
			deliverTypeList.add(new SelectItem(a, a.getDeliveryTypeName()));
		}
		paymentTypeList = new ArrayList<SelectItem>();
		for (PaymentType a : getService().getDao().findAllPaymentType()) {
			paymentTypeList.add(new SelectItem(a, a.getPaymentTypeName()));
		}
		
	}
	
	public void selectDelieveryType(){
		showSelectDelieverTypePopup=true;
	}
	public void selectPaymentType(){
		showSelectPaymentTypePopup=true;
	}
	
	public void selectCustomer(){
		showSelectCustomerPopup=true;
		
	}
	
	public void selectedCustomer(){
		
		if(customerData.isRowAvailable()){
			Customer tempCustomer =(Customer)customerData.getRowData();
			if(orderApply.getCustomer()==null){//首次选择客户
			orderApply.setCustomer(tempCustomer);
			}else if(orderApply.getCustomer()!=null &&orderApply.getCustomer().getCustomerId()!=tempCustomer.getCustomerId()){
				orderApply.setCustomer(tempCustomer);
				this.selectedProducts =new ArrayList<ProductInfo>();
				this.orderProductList =new ArrayList<OrderProductDetails>();
				showProductsData.setWrappedData(orderProductList);
			}
		}
		
		recieverInfoList = new ArrayList<SelectItem>();
		for (RecieverInfo a : getService().getDao().findAllRecieverInfo(orderApply.getCustomer())) {
			recieverInfoList.add(new SelectItem(a, a.getRecieverName()));
		}
		
		selectedProducts =new ArrayList<ProductInfo>();
		productsList=new ArrayList<SelectItem>();
		this.showSelectCustomerPopup=false;
	    
	}

	public void initQueryLists(){
		queryCustomerList = new ArrayList<SelectItem>();
		queryCustomerList.add(new SelectItem(null, "全部"));
		queryCustomerList.addAll(customerList);
		queryAreaList = new ArrayList<SelectItem>();
		queryAreaList.add(new SelectItem(null, "全部"));
		queryAreaList.addAll(areaList);
		queryRecieverInfoList = new ArrayList<SelectItem>();
		queryRecieverInfoList.add(new SelectItem(null, "全部"));
		queryRecieverInfoList.addAll(recieverInfoList);
		queryDeliverTypeList = new ArrayList<SelectItem>(); 
		queryDeliverTypeList.add(new SelectItem(null, "全部"));
		queryDeliverTypeList.addAll(deliverTypeList);
		queryPaymentTypeList = new ArrayList<SelectItem>();
		queryPaymentTypeList.add(new SelectItem(null, "全部"));
		queryPaymentTypeList.addAll(paymentTypeList);
	}

	public DBDataModel getOrderApplyData() {
		return orderApplyData;
	}

	public void setOrderApplyData(DBDataModel orderApplyData) {
		this.orderApplyData = orderApplyData;
	}

	public OrderApply getOrderApply() {
		return orderApply;
	}

	public void setOrderApply(OrderApply orderApply) {
		this.orderApply = orderApply;
	}

	public OrderApplyCondition getOrderApplyCondition() {
		return orderApplyCondition;
	}

	public void setOrderApplyCondition(OrderApplyCondition orderApplyCondition) {
		this.orderApplyCondition = orderApplyCondition;
	}

	public boolean isShowBack() {
		return showBack;
	}

	public void setShowBack(boolean showBack) {
		this.showBack = showBack;
	}

	public List<SelectItem> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<SelectItem> customerList) {
		this.customerList = customerList;
	}

	public List<SelectItem> getQueryCustomerList() {
		return queryCustomerList;
	}

	public void setQueryCustomerList(List<SelectItem> queryCustomerList) {
		this.queryCustomerList = queryCustomerList;
	}

	public String getTabPlacement() {
		return tabPlacement;
	}

	public void setTabPlacement(String tabPlacement) {
		this.tabPlacement = tabPlacement;
	}

	public boolean isShowSelectCustomerPopup() {
		return showSelectCustomerPopup;
	}

	public void setShowSelectCustomerPopup(boolean showSelectCustomerPopup) {
		this.showSelectCustomerPopup = showSelectCustomerPopup;
	}

	public boolean isShowSelectDelieverTypePopup() {
		return showSelectDelieverTypePopup;
	}

	public void setShowSelectDelieverTypePopup(boolean showSelectDelieverTypePopup) {
		this.showSelectDelieverTypePopup = showSelectDelieverTypePopup;
	}

	public boolean isShowSelectPaymentTypePopup() {
		return showSelectPaymentTypePopup;
	}

	public void setShowSelectPaymentTypePopup(boolean showSelectPaymentTypePopup) {
		this.showSelectPaymentTypePopup = showSelectPaymentTypePopup;
	}

	public DBDataModel getSelectProducts() {
		return selectProducts;
	}

	public void setSelectProducts(DBDataModel selectProducts) {
		this.selectProducts = selectProducts;
	}

	public List<SelectItem> getProductsList() {
		return productsList;
	}

	public void setProductsList(List<SelectItem> productsList) {
		this.productsList = productsList;
	}

	public DBDataModel getCustomerData() {
		return customerData;
	}

	public void setCustomerData(DBDataModel customerData) {
		this.customerData = customerData;
	}

	public List<SelectItem> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<SelectItem> areaList) {
		this.areaList = areaList;
	}

	public List<SelectItem> getQueryAreaList() {
		return queryAreaList;
	}

	public void setQueryAreaList(List<SelectItem> queryAreaList) {
		this.queryAreaList = queryAreaList;
	}

	public List<SelectItem> getRecieverInfoList() {
		return recieverInfoList;
	}

	public void setRecieverInfoList(List<SelectItem> recieverInfoList) {
		this.recieverInfoList = recieverInfoList;
	}

	public List<SelectItem> getQueryRecieverInfoList() {
		return queryRecieverInfoList;
	}

	public void setQueryRecieverInfoList(List<SelectItem> queryRecieverInfoList) {
		this.queryRecieverInfoList = queryRecieverInfoList;
	}

	public List<SelectItem> getDeliverTypeList() {
		return deliverTypeList;
	}

	public void setDeliverTypeList(List<SelectItem> deliverTypeList) {
		this.deliverTypeList = deliverTypeList;
	}

	public List<SelectItem> getQueryDeliverTypeList() {
		return queryDeliverTypeList;
	}

	public void setQueryDeliverTypeList(List<SelectItem> queryDeliverTypeList) {
		this.queryDeliverTypeList = queryDeliverTypeList;
	}

	public List<SelectItem> getPaymentTypeList() {
		return paymentTypeList;
	}

	public void setPaymentTypeList(List<SelectItem> paymentTypeList) {
		this.paymentTypeList = paymentTypeList;
	}

	public List<SelectItem> getQueryPaymentTypeList() {
		return queryPaymentTypeList;
	}

	public void setQueryPaymentTypeList(List<SelectItem> queryPaymentTypeList) {
		this.queryPaymentTypeList = queryPaymentTypeList;
	}

	public SelectCustomerCondition getCustomerCondition() {
		return customerCondition;
	}

	public void setCustomerCondition(SelectCustomerCondition customerCondition) {
		this.customerCondition = customerCondition;
	}


	public List<OrderProductDetails> getOrderProductList() {
		return orderProductList;
	}

	public void setOrderProductList(List<OrderProductDetails> orderProductList) {
		this.orderProductList = orderProductList;
	}

	public OrderProductDetails getOrderProduct() {
		return orderProduct;
	}

	public void setOrderProduct(OrderProductDetails orderProduct) {
		this.orderProduct = orderProduct;
	}

	public boolean isShowSelectProduct() {
		return showSelectProduct;
	}

	public void setShowSelectProduct(boolean showSelectProduct) {
		this.showSelectProduct = showSelectProduct;
	}

	public ProxyProductCondition getOrderProductCondition() {
		return orderProductCondition;
	}

	public void setOrderProductCondition(ProxyProductCondition orderProductCondition) {
		this.orderProductCondition = orderProductCondition;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public DBDataModel getShowProductsData() {
		return showProductsData;
	}

	public void setShowProductsData(DBDataModel showProductsData) {
		this.showProductsData = showProductsData;
	}

	public List<ProductInfo> getSelectedProducts() {
		return selectedProducts;
	}

	public void setSelectedProducts(List<ProductInfo> selectedProducts) {
		this.selectedProducts = selectedProducts;
	}

	public boolean isSaved() {
		return saved;
	}

	public void setSaved(boolean saved) {
		this.saved = saved;
	}

	public boolean isShowdetails() {
		return showdetails;
	}

	public void setShowdetails(boolean showdetails) {
		this.showdetails = showdetails;
	}


}
